package com.ruoyi.ysg.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.ysg.domain.YsgCourseSchedule;
import com.ruoyi.ysg.domain.YsgCourseStudent;
import com.ruoyi.ysg.domain.YsgStudent;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ysg.domain.YsgCourse;
import com.ruoyi.ysg.service.IYsgCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 *
 * @author xsz
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/ysg/course")
public class YsgCourseController extends BaseController
{
    @Autowired
    private IYsgCourseService ysgCourseService;

    /**
     * 查询课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(YsgCourse ysgCourse)
    {
        startPage();
        List<YsgCourse> list = ysgCourseService.selectYsgCourseList(ysgCourse);
        return getDataTable(list);
    }


    /**
     * 查询课程关联学生列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:list')")
    @GetMapping("/associatedStudentList")
    public TableDataInfo associatedStudentList(YsgCourseStudent ysgCourseStudent)
    {
        ysgCourseStudent.setAssociated("associated");
        startPage();
        List<YsgStudent> list = ysgCourseService.selectAssociatedStudentList(ysgCourseStudent);
        return getDataTable(list);
    }

    /**
     * 查询课程关联学生列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:list')")
    @GetMapping("/unAssociatedStudentList")
    public TableDataInfo unAssociatedStudentList(YsgCourseStudent ysgCourseStudent)
    {
        ysgCourseStudent.setAssociated("unAssociated");
        startPage();
        List<YsgStudent> list = ysgCourseService.selectAssociatedStudentList(ysgCourseStudent);
        return getDataTable(list);
    }

    /**
     * 查询课表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:list')")
    @PostMapping("/getCourseSchedule")
    public AjaxResult getCourseSchedule(@RequestBody YsgCourseSchedule ysgCourseSchedule)
    {
        return AjaxResult.success(ysgCourseService.getCourseSchedule(ysgCourseSchedule));

    }

    /**
     * 查询课表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:add')")
    @PostMapping("/addCourseSchedule")
    public AjaxResult addCourseSchedule(@RequestBody String jsonStr)
    {
        ysgCourseService.addCourseSchedule(jsonStr);
        return AjaxResult.success();

    }

    /**
     * 导出课程管理列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YsgCourse ysgCourse)
    {
        List<YsgCourse> list = ysgCourseService.selectYsgCourseList(ysgCourse);
        ExcelUtil<YsgCourse> util = new ExcelUtil<YsgCourse>(YsgCourse.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ysgCourseService.selectYsgCourseById(id));
    }

    /**
     * 新增课程管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YsgCourse ysgCourse)
    {
        return toAjax(ysgCourseService.insertYsgCourse(ysgCourse));
    }

    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YsgCourse ysgCourse)
    {
        return toAjax(ysgCourseService.updateYsgCourse(ysgCourse));
    }

    /**
     * 添加学生
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:edit')")
    @Log(title = "课程添加学生", businessType = BusinessType.UPDATE)
    @PutMapping("/associateStudent")
    public AjaxResult associateStudent(@RequestBody YsgCourseStudent ysgCourseStudent)
    {
        ysgCourseStudent.setAssociated("associate");
        return toAjax(ysgCourseService.AssociateStudent(ysgCourseStudent));
    }

    /**
     * 移除学生
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:edit')")
    @Log(title = "课程移除学生", businessType = BusinessType.UPDATE)
    @PutMapping("/unAssociateStudent")
    public AjaxResult unAssociateStudent(@RequestBody YsgCourseStudent ysgCourseStudent)
    {
        ysgCourseStudent.setAssociated("unAssociate");
        return toAjax(ysgCourseService.AssociateStudent(ysgCourseStudent));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ysgCourseService.deleteYsgCourseByIds(ids));
    }
}
